package com.qingfeng.core.aop.exception;

import com.qingfeng.core.config.redis.RedisUtil;
import com.qingfeng.core.constant.RedisKeyConstant;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @description: 异常切面类
 * @author:LWY
 * @date: 2017/11/10 9:52
 * @modified:
 */
@Aspect
@Component
public class HttpAspect {
    public static final Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Value("${my.platform}")
    public Integer platform;

    @Pointcut("execution(public * com.qingfeng.*.controller.*.*(..))")
    public void point() {

    }

    /**
     * 环绕通知
     *
     * @param proceedingJoinPoint
     * @throws Throwable
     */
    @Around("point()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //class_method
        String class_name = proceedingJoinPoint.getSignature().getDeclaringTypeName();
        String method_name = proceedingJoinPoint.getSignature().getName();
        //打印日志
        logger.info("请求的class和method={}", class_name + "." + method_name);
        logger.info("参数={}", proceedingJoinPoint.getArgs());//args[]

        if (platform == 2) {
            /**
             * 客户端的请求
             *  除了（登陆接口、注册接口、登陆验证码、注册验证码、找回密码），都需要token验证身份
             */
            if (!notToken(class_name + "_" + method_name)) {
                ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = servletRequestAttributes.getRequest();
                //用户id
                String id = request.getParameter("id");
                //token
                String token = request.getParameter("token");
                //服务器的token
                String service_token = RedisUtil.getStr(RedisKeyConstant.USER_TOKEN_ID + id);
                if (StringUtils.isEmpty(token) || StringUtils.isEmpty(service_token)) {
                    throw new HttpException("您好，请重新登录");
                }
                if (!Objects.equals(token, service_token)) {
                    throw new HttpException("您好，请重新登录");
                }
            }
        }
        return proceedingJoinPoint.proceed();
    }


    /**
     * 不需要验证的身份的url
     *
     * @param class_method 请求的class和method,下划线隔开
     * @return
     */
    public boolean notToken(String class_method) {
        //登录
        if (Objects.equals(class_method, "com.qingfeng.provide.controller.UserController_login")) {
            return true;
        }
        //发送验证码
        if (Objects.equals(class_method, "com.qingfeng.provide.controller.UserController_auth")) {
            return true;
        }
        //忘记密码
        if (Objects.equals(class_method, "com.qingfeng.provide.controller.UserController_forget")) {
            return true;
        }
        return false;
    }

}
